package com.iwhalecloud.bss.kite.cucc.service.job.scene;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.scene.IInitSceneOfferInstService;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosGoodsDTO;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.service.context.ServiceContext;
import com.ztesoft.bss.common.bo.LoginInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.common.logger.api.ZLogger;
import com.ztesoft.common.logger.factory.ZLoggerFactory;

public class OfferInstReloadTask implements Callable<Map<String, Object>> {
    private static final ZLogger LOGGER = ZLoggerFactory.getLogger(OfferInstReloadTask.class, KeyConsts.LOG_MODULE);

    private String sessionId;

    private LoginInfo loginInfo;

    private List<KitePosGoodsDTO> goodsList;

    private String sceneInstId;
    private String operType;
    private String custId;
    private String staffId;

    public OfferInstReloadTask(String sceneInstId, String operType, String custId, String staffId, List<KitePosGoodsDTO> goodsList, String sessionId, LoginInfo loginInfo) {
        this.sceneInstId = sceneInstId;
        this.operType = operType;
        this.custId = custId;
        this.staffId = staffId;
        this.goodsList = goodsList;
        this.sessionId = sessionId;
        this.loginInfo = loginInfo;
    }

    @Override
    public Map<String, Object> call() throws Exception {
        IInitSceneOfferInstService initSceneOfferInstService = SpringUtil.getBean(IInitSceneOfferInstService.class);
        // 礼包处理
        try {
            if (!KiteListUtils.isEmpty(this.goodsList) && this.goodsList.stream().allMatch(goods -> KiteStringUtils.equals(OfferCache.get(goods.getCode()).getOfferType(), KeyConsts.OFFER_TYPE_15))) {
                initSceneOfferInstService.initGiftOfferInst(sceneInstId, custId, staffId, this.goodsList, loginInfo);
            }
            else {
                initSceneOfferInstService.initMainOfferInst(sceneInstId, custId, staffId, this.goodsList, loginInfo);
                initSceneOfferInstService.initAddRoleOfferInst(sceneInstId, custId, staffId, this.goodsList, loginInfo);
                initSceneOfferInstService.initAdditionalOfferInst(sceneInstId, custId, staffId, this.goodsList, loginInfo);
            }
        }
        catch(Exception e) {
            LOGGER.error(ErrorConsts.SYSTEM_ERROR.getErrorCode(), e.getCause(),e.getMessage());
            ErrorConsts.INF_SALES_HANDLE_EXCEPTION.throwOut(e.getMessage());
        }

        return new HashMap<String, Object>();
    }

}
